<?php

/**
 * 微信排队叫号系统主程序
 * Author: lishengcn.cn <eric@lishengcn.cn>
 * Time: 2014-11-3 04:31
 */

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{   
    /**
     * 验证签名
     */
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature){
            echo $echoStr;
            exit;
        }
    }

    /**
     * 响应用户输入的text类型消息
     */
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)) {
            
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            if ($RX_TYPE == "text") {
                $result = $this->receiveText($postObj);
            }
            
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    /**
     * 接受事件消息
     */
    private function receiveEvent($object)
    {
        $content = "";
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注";
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
            default:
                $content = "receive a new event: ".$object->Event;
                break;
        }
        
        $result = $this->transmitText($object, $content);
        
        return $result;
    }


    /**
     * 接收文本消息
     */
    private function receiveText($object)
    {
        $keyword = trim($object->Content);

        if (strstr($keyword, "排队")) {
            $c = new SaeCounter();
            $jiao = $c->get('Queuing'); 

            $mysql = new SaeMysql();

            $sql = "select * from Queuing where q_openid = '{$object->FromUserName}'";
            $data = $mysql->getData($sql);
            $user = $data[0][q_openid];

            if (empty($user)) {
                $sql1 = "insert into Queuing values (NULL, '{$object->FromUserName}')";
                $mysql->runSql($sql1);
                $sql = "select * from Queuing where q_openid = '{$object->FromUserName}'";
                $data = $mysql->getData($sql);
            }

            $id = $data[0][q_id];
            $id = intval($id);

            $num = $id-$jiao;
            $content = "你的排号是 " . $id . "\r\n当前是 " . $jiao . " 号，\r\n前面还有 " . $num . " 人";
            //$content = var_dump($user);
        }else{
            $content = '请输入"排队"';
        }

        $result = $this->transmitText($object, $content);

        return $result;
    }

    /**
     * 回复文本消息
     */
    private function transmitText($object, $content)
    {
        $xmlTpl = "<xml>
                   <ToUserName><![CDATA[%s]]></ToUserName>
                   <FromUserName><![CDATA[%s]]></FromUserName>
                   <CreateTime>%s</CreateTime>
                   <MsgType><![CDATA[text]]></MsgType>
                   <Content><![CDATA[%s]]></Content>
                   </xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        
        return $result;
    }

    
}
?>